package com.chii.colorization.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chii.colorization.R;

import java.util.HashMap;
import java.util.List;

public class ColorAdapter extends BaseAdapter {
    private Context context;
    private List<HashMap<String, Object>> imageList;

    public ColorAdapter(Context context, List<HashMap<String, Object>> imageList) {
        this.context = context;
        this.imageList = imageList;
    }

    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public Object getItem(int position) {
        return imageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.listview_color, null);
            viewHolder.imageViewInput = convertView.findViewById(R.id.imageView_input);
            viewHolder.imageViewOutput = convertView.findViewById(R.id.imageView_output);
            convertView.setTag(viewHolder);
        } else {
            //获取viewHolder实例
            viewHolder = (ViewHolder) convertView.getTag();
        }
        glideUtil(viewHolder.imageViewInput, imageList.get(position).get("input"));
        glideUtil(viewHolder.imageViewOutput, imageList.get(position).get("output"));
        return convertView;
    }

    static class ViewHolder {
        ImageView imageViewInput;
        ImageView imageViewOutput;
    }

    private void glideUtil(ImageView imageView, Object imaage) {
        Glide.with(imageView.getContext())
                .load(imaage)
                .dontAnimate()
                .thumbnail( 0.1f )
                .placeholder(R.drawable.ic_autorenew_black)
                .into(imageView);
    }

}
